import {queryFakeList, removeFakeList, addFakeList, updateFakeList} from '@/services/api';
import * as articleService from '../services/article'
import * as categoryService from '../services/category'
import * as routerRedux from "react-router-redux";
import {message} from "antd";
import {stringify} from "qs";
import pathToRegexp from 'path-to-regexp';


export default {
  namespace: 'article',
  state: {
    list: [],
    detail: {},
    categoryList: [],
    countNum: 500,
    pageData: {
      size: 10,
      page: 1
    },
    searchValues:{}
  },

  effects: {
    * fetch({payload}, {call, put}) {
      const response = yield call(articleService.allArticle, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryList',
          payload: {list:Array.isArray(response.data) ? response.data : [],pageData:{size:payload.size,page:payload.start/10+1},searchValues:payload.searchValues},
        });
      } else if(response.msg) {
        message.success(response.msg || "err");
      }
    },
    * save({payload}, {call, put}) {
      const addArticleLoading = message.loading('修改文章中', 0);
      setTimeout(() => {
        addArticleLoading();
      }, 5000);
      const response = yield call(articleService.saveAricle, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        addArticleLoading();
        message.success('新增成功');
        yield put(routerRedux.push({
          pathname: '/article/list'
        }));
      } else {
        addArticleLoading();
        message.error(response.msg || "error_1")
      }
    },
    * detail({payload}, {call, put}) {
      const response = yield call(articleService.detailArticle, payload);
      if(!response){
        return false;
      }
      yield put({
        type: 'queryDetail',
        payload: response.data ? response.data : {},
      });
    },
    * fetchCategory({payload}, {call, put}) {
      const response = yield call(categoryService.allCategory, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryCategory',
          payload: Array.isArray(response.data) ? response.data : [],
        });
      } else {
        message.error("获取文章类别失败！")
      }
    },
    * changeArticleState({payload}, {call, put}) {
      const response = yield call(articleService.changeArticleState, payload);
      if(!response){
        return false;
      }
      const changeStatueLoading = message.loading('修改文章状态中', 0);
      setTimeout(() => {
        changeStatueLoading();
      }, 5000);
      if (response.code == "SUCCESS") {
        changeStatueLoading();
        message.success("修改成功")
        yield put({
          type: 'fetch',
          payload: {...payload.pageData, ...payload.sortData, ...payload.searchValues}
        });
      } else {
        changeStatueLoading();
        message.error(response.msg || "error_1")
      }
    },
    * delete({payload}, {call, put}) {
      const response = yield call(articleService.deleteArticle, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        message.success("删除成功");
        yield put({
          type: 'fetch',
          payload: {...payload.pageData, ...payload.sortData, ...payload.searchValues}
        });
      } else {
        message.error(response.msg || "error_1")
      }
    },
    * queryCount({payload}, {call, put}) {
      const response = yield call(articleService.countArticle, payload);
      if(!response){
        return false;
      }
      if (response.code == "SUCCESS") {
        yield put({
          type: 'queryCountNum',
          payload: response.data ? response.data : 0,
        });
      } else {
        message.error(response.msg || "error_1")
      }
    },
  },
  reducers: {
    queryList(state, action) {
      return {
        ...state,
        list: action.payload.list,
        pageData: action.payload.pageData,
        searchValues:action.payload.searchValues
      };
    },
    queryDetail(state, action) {
      console.log("queryDetail===>" + JSON.stringify(action.payload))
      return {
        ...state,
        detail: action.payload,
      };
    },
    queryCategory(state, action) {
      return {
        ...state,
        categoryList: action.payload,
      };
    },
    queryCountNum(state, action) {
      return {
        ...state,
        countNum: action.payload,
      };
    },
  },
  subscriptions: {
    setup({dispatch, history}) {
      return history.listen(({pathname}) => {
        // const matchList = pathToRegexp('/article/list').exec(pathname);
        const matchDetail = pathToRegexp('/article/detail/:id').exec(pathname);
        const matchEdit = pathToRegexp('/article/edit/:deviceId').exec(pathname);
        if (matchDetail) {
          const articleId = matchDetail[1];
          dispatch({type: 'detail', payload: articleId});
        } else if (matchEdit) {
          const articleId = matchEdit[1];
          dispatch({type: 'detail', payload: articleId});
        }
      });
    },
  }
}
